package com.libc.yygh.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * * 全局统一返回结果类
 * Created with Description:
 *
 * @fileRelativePath: Result.java
 * @Date: Created on 2022/7/19  11:14  <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
@Data
@ApiModel (value = "全局统一返回结果")
public class Result< T >
{
    @ApiModelProperty (value = "返回码")
    private Integer code;

    @ApiModelProperty (value = "返回消息")
    private String message = "请求成功！";
    private String msg = "请求成功！";

    @ApiModelProperty (value = "返回数据")
    private T data;

    public Result( )
    {
        code = ResultCodeEnum.SUCCESS.getCode();
        message = ResultCodeEnum.SUCCESS.getMessage();
    }

    protected static < T > Result< T > build(T data)
    {
        Result< T > result = new Result< T >();
        if (data != null)
        { result.setData(data); }
        return result;
    }

    public static < T > Result< T > build(T body, ResultCodeEnum resultCodeEnum)
    {
        Result< T > result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setMsg(resultCodeEnum.getMessage());
        return result;
    }

    public static < T > Result< T > build(Integer code, String message)
    {
        Result< T > result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static < T > Result< T > ok( )
    { return Result.ok(null); }

    /************************************************************* <br>
     *
     *说明: 操作成功<br>
     *
     * @Description   <br>
     * @Date Created on 2022/7/19  11:17  <br>
     * @param data <br>
     * @return com.xisoft.base.Result<T>  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static < T > Result< T > ok(T data)
    {
        Result< T > result = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static < T > Result< T > fail( )
    {
        return Result.fail(null);
    }

    /**
     * 操作失败
     *
     * @param data
     * @param <T>
     * @return
     */
    public static < T > Result< T > fail(T data)
    {
        Result< T > result = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }

    public Result< T > message(String msg)
    {
        this.setMessage(msg);
        return this;
    }

    public Result< T > code(Integer code)
    {
        this.setCode(code);
        return this;
    }

    public boolean isOk( )
    {
        if (this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue())
        { return true; }
        return false;
    }
}
